package cn.mjnxin.iot.client.test;

import java.util.Scanner;

import cn.mjnxin.iot.base.util.MixUtil;
import io.netty.buffer.Unpooled;

/**
 * 循环发送
 * @author CLG 2023年10月10日
 * @version V1.0.0
 */
public class HexMessageSender implements Runnable{
    private  static Scanner userInput = new Scanner(System.in);
    
    private final ITerminalDataSender sender;

    public HexMessageSender(ITerminalDataSender clientDataSender) {
        this.sender = clientDataSender;
    }
    
    private void loopSend() {
        new Thread() {public void run() {
        sendStart("FAF5000311021002019000000471011010");
        while(true) {
            try {
                Thread.sleep(5000l);
            } catch (InterruptedException e) {
                break;
            }
//            sendStart("FAF50003110370020190000001061CC3C3");
            sendStart("FAF5000811021002019000000471054802011A19007E");
            try {
                Thread.sleep(5000l);
            } catch (InterruptedException e) {
                break;
            }
            sendStart("FAF5001A11021002019000000471043C000000001B190000000000000000001413010C0A0B0CFFC4");
//            sendStart("FAF5000311021002019000000471011010");
//电能：E00000000006750327100020      E00000000006020327100008   E00000000006010327100020
//应答：E0000000004375034000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
//遥测：E00000000006750300000068                                 E00000000006010300000030
//应答：E000000000D37503D00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003F8000003F8000003F8000003F800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000            
        }
    }}.start();
    }
    
    @Override
    public void run() {
        printHelp();
        String hexStr = "";
        while(!"#".equals(hexStr)) {
        	try {
	            hexStr = userInput.nextLine();
	            if ("*".equals(hexStr)) {
	                printHelp();
	                continue;
	            }
	            if ("S".equals(hexStr)) {
	                loopSend();
	                continue;
	            }
	            if (MixUtil.isHex(hexStr)) {
	                sendStart(hexStr.toUpperCase());
	            }
	            else {
	                sendStart(hexStr.getBytes());
	            }
        	}
        	catch(Exception ex) {
        		ex.printStackTrace();
        	}
        }
        
        System.exit(0);
    }
    
    private static void printHelp() {
        System.out.println("粘贴十六进制串发送报文");
        System.out.println("* 帮助");
        System.out.println("S 循环发送");
        System.out.println("# 退出");
    }
    
    private void sendStart(String hexStr) {
        byte[] data  = MixUtil.hex2bytes(hexStr);
        try {
            sender.sendMessage(Unpooled.wrappedBuffer(data));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    private void sendStart(byte[] data) {
        try {
            sender.sendMessage(Unpooled.wrappedBuffer(data));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
